package com.fuyao.cloud.personnel.library.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.biz.dto.PageQuery;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.library.entity.BookCategory;
import com.fuyao.cloud.personnel.library.service.BookCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/bookCategory")
@Api(value = "book", tags = "书籍信息管理")
public class BookCategoryController {

    private final BookCategoryService bookCategoryService;

    /**
     * 分页查询
     *
     * @param pageQuery 分页对象
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page")
//    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Page<BookCategory>> getPage(PageQuery<BookCategory> pageQuery) {
        return R.ok(bookCategoryService.getPage(pageQuery));
    }

    @GetMapping("/tree")
    public R<List<Tree<Long>>> getTree() {
        return R.ok(bookCategoryService.selectTree());
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<BookCategory> getById(@PathVariable("id") Long id) {
        return R.ok(bookCategoryService.getById(id));
    }

    /**
     * 新增
     *
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<BookCategory> save(@RequestBody BookCategory book) {
        return R.ok(bookCategoryService.saveCategory(book));
    }

    /**
     * 修改
     *
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<BookCategory> updateById(@RequestBody BookCategory category) {
        return R.ok(bookCategoryService.updateCategory(category));
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(bookCategoryService.removeById(id));
    }


    /**
     * 导出 excel 表格
     *
     * @param book 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public List<BookCategory> export(BookCategory book) {
        return bookCategoryService.list(Wrappers.query(book));
    }

    /**
     * 创建方法
     */
    @GetMapping(value = "/create")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<BookCategory> create() {
        return R.ok(bookCategoryService.create());
    }

    /**
     * 批量删除方法
     *
     * @return R
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @SysLog("批量删除")
    @DeleteMapping("/batchDelete")
    @PreAuthorize("@pms.hasPermission('book-admin')")
    public R<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return R.ok(bookCategoryService.removeByIds(ids));
    }
}
